/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.utils;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;

public class JsonUtil {
    public static Long getLong(JsonObject o, String key) {
        Object value = o.getValue(key);
        if (value == null) {
            return 0l;
        }
        if (value instanceof Number) {
            if (value instanceof Long) {
                return (Long) value;
            } else {
                return ((Number) value).longValue();
            }
        } else {
            try {
                return Long.parseLong(value.toString());
            } catch (Exception e) {
                return 0l;
            }
        }
    }

    public static Integer getInteger(JsonObject o, String key) {
        Object value = o.getValue(key);
        if (value == null) {
            return 0;
        }
        if (value instanceof Number) {
            if (value instanceof Integer) {
                return (Integer) value;
            } else {
                return ((Number) value).intValue();
            }
        } else {
            try {
                return Integer.parseInt(value.toString());
            } catch (Exception e) {
                return 0;
            }
        }
    }

    public static String getString(JsonObject o, String key) {
        Object value = o.getValue(key);
        if (value == null) {
            return "";
        }
        return value.toString();
    }

    public static JsonObject toStrVal(JsonObject jsonObject) {
        jsonObject.stream()
                .forEach(e->{
                    Object value = e.getValue();
                    if (value instanceof Number) {
                        e.setValue(value.toString());
                    }
                });
        return jsonObject;
    }

    public static JsonArray toStrVal(JsonArray jsonArray) {
        jsonArray.stream().forEach(e->toStrVal((JsonObject)e));
        return jsonArray;
    }


    public static boolean simpleCheckJSONType(String str) {
        boolean result = false;
        if (StringUtils.isNotBlank(str)) {
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }
}
